<?php

namespace app\api\controller;

use app\admin\model\Advice as ModelAdvice;
use app\common\controller\Api;
use think\Db;
use think\Exception;

class Advice extends Api
{
    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = ['*'];

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = ['*'];
    /**
     * 意见反馈提交接口
     */
    public function addAdvice()
    {
        $user_id = $this->auth->id;
        // $user_id = 1;
        if (!$user_id) {
            $this->error('请登录');
        }
        $params = $this->request->post();
        if (!$params['content']) {
            $this->error('内容不能为空');
        }
        $options = [
            'user_id' => $user_id,
            'content' => $params['content'],
            'images' => $params['images'] ?? '',
            'createtime' => time(),
        ];
        Db::startTrans();
        try {
            $row = ModelAdvice::create($options);
            if ($row) {
                Db::commit();
                $this->success('提交成功,感谢您的反馈!');
            } else {
                throw new Exception('提交失败');
            }
        } catch (Exception $e) {
            Db::rollback();
            $this->error('提交失败');
        }
    }
}
